/*
 * 代号：凤凰
 * 创建日期 2007-6-22
 * V1.0
 */
package com.xsoft.framework.util;


/**
 * 字符串处理工具
 * @author 刘虻
 * 2007-6-22下午04:25:21
 */
public class StringHtmlUtil {

	
	/**
	 * 将变量中的html内容经过整理,输出到html页面的textarea中
	 * @author 刘虻
	 * 2007-10-12下午07:59:18
	 * @param html 内容
	 * @return 整理后的内容
	 */
	public static String fixOutHtmlToTextarea(String html) {
		
		if (html==null) {
			return null;
		}
		return html
					.replaceAll("(\\&)","&amp;")
					.replaceAll("(?i)<textarea","&lt;textarea")
					.replaceAll("(?i)</textarea","&lt;/textarea");
	}
	
	
	/**
	 * 处理html内容 将敏感字符替换成安全字符
	 * @author 刘虻
	 * 2007-10-17下午05:53:03
	 * @param html 源html内容
	 * @return 处理后的html内容
	 */
	public static String fixHtmlString(String html) {
		
		if (html==null) {
			return null;
		}
		return html
					.replaceAll("(\\&)","&amp;")
					.replaceAll("(\\#)","&#35;")
					.replaceAll("(\\?)","&#63;")
					.replaceAll("(\\%)","&#37;")
					.replaceAll("(?i)<","&lt;")
					.replaceAll("(?i)>","&gt;")
					.replaceAll("(?i) ","&nbsp;")
					.replaceAll("(?i)\\r\\n","<br/>")
					.replaceAll("(?i)\\r","<br/>")
					.replaceAll("(?i)\\n","<br/>");
	}
	
	/**
	 * 将html内容转换为字符串类型
	 * @author 刘虻
	 * 2007-10-22下午07:57:20
	 * @param html html内容
	 * @return 转换后的字符串
	 */
	public static String unFixHtmlString(String html) {
		if (html==null) {
			return null;
		}
		return html
					.replaceAll("(?i)&amp;","&")
					.replaceAll("(?i)&#35;","#")
					.replaceAll("(?i)&#63;","?")
					.replaceAll("(?i)&#37;","%")
					.replaceAll("(?i)&lt;","<")
					.replaceAll("(?i)&gt;",">")
					.replaceAll("(?i)&nbsp;"," ")
					.replaceAll("(?i)<br/>","\r\n");
	}
	
	
	/**
	 * 测试入口
	 * @author 刘虻
	 * 2007-6-22下午04:25:22
	 * @param args 导入参数
	 */
	public static void main(String[] args) {
		
		//.replaceAll("(\\&)","&amp;")
		//System.out.println(cutHtmlString("[<b>一版</b>]<font color='red'>华安180再次分红  2003年累计分红每10份0.7元</font>",20,"..."));
		//System.out.println(getURLEncoding("http://www.sina.com.cn?测试=^_^"));
		//System.out.println(cutHtmlString("测试测试测试测试测试测试测试测试<br/>测试测试测试测试<br/>测试测试测试测试测试测试<br/>",5,"..."));
		//String a = getURLEncoding("http://localhost:8088/fundweb0804/im/artsearchlist.ha?keywords=哈啊");
		//a = a.replaceAll("%2F","/").replaceAll("%3A",":").replaceAll("%3F","?").replaceAll("%3D","=").replaceAll("%26","&");
		//System.out.println(fixNumberString("-3.232.2323_"));
		//System.out.println(getI18nString("基金"));
		
		System.out.println(getHtmlText("<P class=MsoNormal style='MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20pt; mso-char-indent-count: 2.0'><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: 宋体'><FONT color=#000000>由于一系列负面经济数据的影响，国际油价在前一个交易日暴涨９％之后２５日遭遇重挫，纽约市场油价下跌近７％，重回每桶５０美元水平。<SPAN lang=EN-US><o:p></o:p></SPAN></FONT></SPAN></P>"));
		try {
			//System.out.println(new String("\u57fa\u91d1".getBytes("UTF-8")));
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 获取html中的文本内容
	 * @author 刘虻
	 * 2009-2-4下午02:59:30
	 * @param source html内容
	 * @return 文本内容
	 */
	public static String getHtmlText(String source) {
		return cutHtmlReturnText(source,-1,null);
	}
	
	/**
	 * 将html内容转换为文本内容，并针对文本内容截取字符串
	 * @author 刘虻
	 * 2009-2-4下午02:57:01
	 * @param source html内容
	 * @param cutSize 截取字符数
	 * @param footStr 截取后在结尾增加的字符串
	 * @return 处理后的字符串
	 */
	public static String cutHtmlReturnText(
			String source,int cutSize,String footStr) {
		
		if (source==null) {
			return "";
		}
		if (footStr==null) {
			footStr = "";
		}
		int length = source.length(); //内容长度
		if (cutSize<1 || length<cutSize) {
			cutSize = length;
		}
		if (source.indexOf("<")<0) {
			return source.substring(0,cutSize)+footStr;
		}
		int readPoint = 0;
		//构造返回值
		StringBuffer reSbf = new StringBuffer();
		int tPoint = 0; //标签标志数
		for(int i=0;i<length;i++) {
			//获取一个字符
			String oneChar = source.substring(i,i+1);
			if (oneChar.equals("<")) {
				tPoint++;
				continue;
			}else if (oneChar.equals(">")) {
				tPoint--;
				continue;
			}
			if (tPoint==0) {
				reSbf.append(oneChar);
				readPoint++;
				if (readPoint>cutSize) {
					break;
				}
			}
		}
		return reSbf.append(footStr).toString().replaceAll("&nbsp;","");
	}
	
	
	/**
	 * 限制字符串长度（不算html代码）
	 * @author 刘虻
	 * 2007-10-15下午08:02:28
	 * @param source 源html字符串
	 * @param cutSize 限制长度
	 * @param footStr 限制后附加字符串
	 * @return
	 */
	public static String cutHtmlString(String source,int cutSize,String footStr) {
		if (source==null) {
			return "";
		}
		if (footStr==null) {
			footStr = "";
		}
		int length = source.length();
		if (length<=cutSize) {
			return source;
		}
		
		if (source.indexOf("<")<0) {
			return source.substring(0,cutSize)+footStr;
		}
		
		
		int readPoint = 0;
		//构造返回值
		StringBuffer reSbf = new StringBuffer();
		
		boolean isAppend = true; //是否累加
		boolean notAppendFoot = true; //是否没放入尾部
		boolean insideTag = false; //是否为标签内部
		boolean onOutTag = false; //是否为结束字符
		for(int i=0;i<length;i++) {
			//获取一个字符
			String oneChar = source.substring(i,i+1);
			if (oneChar.equals("<")) {
				insideTag = true;
			}else if (oneChar.equals(">")) {
				insideTag = false;
				onOutTag = true;
			}else if (!insideTag) {
				readPoint++;
			}
			if (readPoint>cutSize) {
				if (insideTag) {
					isAppend = true;
				}else {
					isAppend = false;
				}
				if(notAppendFoot) {
					reSbf.append(footStr);
					notAppendFoot = false;
				}
			}
			if (isAppend || onOutTag) {
				if (onOutTag) {
					onOutTag = false;
				}
				reSbf.append(oneChar);
			}
		}
		return reSbf.toString().replaceAll("<br/>","").replaceAll("<br>","");
	}
	
	


}
